package com.lh.pospi.x_store.ui.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.lh.pospi.x_store.R;
import com.lh.pospi.x_store.ui.DeleteActivity;
import com.lh.pospi.x_store.ui.inventory.InventoryActivity;
import com.lh.pospi.x_store.ui.overflow.OverflowActivity;
import com.lh.pospi.x_store.ui.purchase.PurchaseActivity;
import com.lh.pospi.x_store.ui.wholesale.WholesaleActivity;
import com.lh.pospi.x_store.utils.Constant;
import com.lh.pospi.x_store.utils.DBService;
import com.lh.pospi.x_store.utils.DownHelper;
import com.lh.pospi.x_store.utils.LoadingUtil;
import com.lh.pospi.x_store.view.ProgressDialog;
import com.vondear.rxtool.view.RxToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PurchaseFragment extends Fragment implements AdapterView.OnItemClickListener {
    @BindView(R.id.gridview)
    GridView gridview_main;
    private SimpleAdapter adapter;
    private List<Map<String, Object>> dataList;
    private ProgressDialog dialog;
    private LoadingUtil util;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_main, null);
        ButterKnife.bind(this,v);
        String[] from={"img","text"};
        int[] to={R.id.img,R.id.text};
        initData();
        adapter=new SimpleAdapter(getActivity(), dataList, R.layout.grid_item, from, to);
        gridview_main.setAdapter(adapter);
        gridview_main.setOnItemClickListener(this);
        return v;
    }

    public static PurchaseFragment getInstance() {
        PurchaseFragment fragment = new PurchaseFragment();
        return fragment;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent;
        switch (dataList.get(i).get("text").toString()) {
            case "采购订单":
                intent = new Intent(getActivity(), PurchaseActivity.class);
                intent.putExtra(Constant.PURCHASE, Constant.TYPE_D1);
                startActivity(intent);
                break;
            case "采购入库":
                intent = new Intent(getActivity(), PurchaseActivity.class);
                intent.putExtra(Constant.PURCHASE, Constant.TYPE_RK);
                startActivity(intent);
                break;
            case "采购退厂":
                intent = new Intent(getActivity(), PurchaseActivity.class);
                intent.putExtra(Constant.PURCHASE, Constant.TYPE_TC);
                startActivity(intent);
                break;
            case "报损单":
                intent = new Intent(getActivity(), OverflowActivity.class);
                intent.putExtra(Constant.OVERFLOW, Constant.TYPE_BS);
                startActivity(intent);
                break;
            case "损溢单":
                intent = new Intent(getActivity(), OverflowActivity.class);
                intent.putExtra(Constant.OVERFLOW, Constant.TYPE_BY);
                startActivity(intent);
                break;
            case "补货单":
                intent = new Intent(getActivity(), InventoryActivity.class);
                intent.putExtra(Constant.INVENTORY, Constant.TYPE_BH);
                startActivity(intent);
                break;
            case "调拨单":
                intent = new Intent(getActivity(), InventoryActivity.class);
                intent.putExtra(Constant.INVENTORY, Constant.TYPE_DB);
                startActivity(intent);
                break;
            case "盘点单":
                intent = new Intent(getActivity(), InventoryActivity.class);
                intent.putExtra(Constant.INVENTORY, Constant.TYPE_PD);
                startActivity(intent);
                break;
            case "批发销售":
                intent = new Intent(getActivity(), WholesaleActivity.class);
                intent.putExtra(Constant.WHOLESALE, Constant.TYPE_PF);
                startActivity(intent);
                break;
            case "批发退货":
                intent = new Intent(getActivity(), WholesaleActivity.class);
                intent.putExtra(Constant.WHOLESALE, Constant.TYPE_TH);
                startActivity(intent);
                break;
            case "删除数据":
                intent = new Intent(getActivity(), DeleteActivity.class);
                startActivity(intent);
                break;
            case "数据更新":
                new AlertDialog.Builder(getActivity())
                        .setTitle("提示")
                        .setMessage("开始更新")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                connectDb();
                            }
                        })
                        .setNegativeButton("取消", null).show();
                break;

        }
    }
    int pro = 0;
    private void connect(){

        dialog = new ProgressDialog(getActivity());
        dialog.setProgress(0);
        dialog.setCancelable(false);
        dialog.show();
        DownHelper helper = new DownHelper();
        helper.getGoodsSize(new DownHelper.CountLister() {
            @Override
            public void getCount(int count) {
                dialog.setMax(count);
                upPro(dialog,count);
            }
        });

//        helper.downGoods();
//        helper.downStock();
//        helper.downUser();
//        helper.downMark();
//        helper.downCus();
//        helper.downSup();
//        helper.downBrand();
//        helper.downCat();
    }
    private void upPro(final ProgressDialog dialog, final int max) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (pro <max) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            dialog.setProgress(pro);
                        }
                    });
                    pro+=400;
                    try {
                        Thread.sleep(1400);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                dialog.dismiss();
                pro = 0;



            }
        }).start();
    }
    private void connectDb() {
        util = new LoadingUtil(getActivity());
        util.show("下载资料中");
        new Thread(new Runnable() {
            @Override
            public void run() {
                DBService.getDbService().saveGoods();
                DBService.getDbService().saveBrand();
                DBService.getDbService().saveCat();
                DBService.getDbService().saveCus();
                DBService.getDbService().saveMark();
                DBService.getDbService().saveSup();
                DBService.getDbService().saveUser();
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        RxToast.normal("更新成功");

                    }
                });
                util.close();
            }
        }).start();
    }

    private void initData() {
        //图标
        int icno[] = {R.mipmap.icon_cgdd,R.mipmap.icon_cgsh,R.mipmap.icon_cgtc,
                R.mipmap.icon_xsdd,R.mipmap.icon_xsck,R.mipmap.icon_sspd,
                R.mipmap.icon_mdbh,R.mipmap.icon_tbck,R.mipmap.icon_spsy,
                R.mipmap.icon_spzl,R.mipmap.icon_sspd,R.mipmap.icon_xsdd};
        //图标下的文字
        String name[]={
                "采购订单","采购入库", "采购退厂",
                "批发销售","批发退货", "盘点单",
                "补货单","调拨单","报损单",
                "损溢单","删除数据","数据更新"};
        dataList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i <icno.length; i++) {
            Map<String, Object> map=new HashMap<String, Object>();
            map.put("img", icno[i]);
            map.put("text",name[i]);
            dataList.add(map);
        }
    }
}
